package com.l.web.service.system.webnode;

import com.l.web.service.KeyUpdate;
import com.l.web.service.ResponseCode;
import com.l.web.service.ServiceSupport;
import org.l.qiao.Cluster;
import org.l.qiao.Validate;
import org.l.qiao.search.Field;
import org.l.qiao.search.Query;
import org.l.qiao.search.Update;
import com.l.web.service.ResponseInfo;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;

import java.util.ArrayList;
import java.util.List;

public class WebNodeUpdateService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(WebNodeUpdateService.class);
    private WebNodeDAO webNodeDAO;

    public void setWebNodeDAO(WebNodeDAO webNodeDAO) {
        this.webNodeDAO = webNodeDAO;
    }

    public String execute() {

        String type = form.getString("type");
        if ("parent".equals(type)) {
            return parent();
        } else {
            return update();
        }
    }

    private String update() {
        KeyUpdate keyUpdate = this.getKeyUpdate(WebNode.class);
        LOGGER.debug("修改信息信息：" + keyUpdate);
        if (keyUpdate.check()) {
            int result = ResponseCode.FINISH;
            try {
                if ("PARENT_ID".equals(keyUpdate.getName())) {
                    result = updateParent(keyUpdate);
                } else {
                    webNodeDAO.update(keyUpdate);
                }
            } catch (Exception e) {
                result = ResponseCode.FAILURE;
                LOGGER.error("修改异常[" + keyUpdate + "]：" + e);
            }
            ResponseInfo msg = new ResponseInfo(result);
            out(msg);
        } else {
            LOGGER.error("非法修改：" + keyUpdate);
            error(404);
        }
        return NONE;
    }

    private int updateParent(KeyUpdate keyUpdate) {
        ResponseInfo ri = new ResponseInfo();
        Cluster webNode = webNodeDAO.findById(keyUpdate.getId());
        String parentId = keyUpdate.getValue();
        List oldParentList = webNode.getList(WebNode.PARENT, new ArrayList());
        List newParentList;
        if (parentId == null || "".equals(parentId)) {
            newParentList = new ArrayList();
            webNode.remove(WebNode.PARENT_ID);
        } else {
            Cluster parent = webNodeDAO.findById(parentId);
            if (parent.isEmpty()) {
                return ResponseCode.EXIST_NOT;
            }
            newParentList = parent.getList(WebNode.PARENT, new ArrayList());
            newParentList.add(parent.get(WebNode.ID));
            webNode.put(WebNode.PARENT_ID, parent.get(WebNode.ID));
        }
        webNode.put(WebNode.PARENT, newParentList);
        webNodeDAO.save(webNode);
        updateChildren(webNode, oldParentList, newParentList);
        return ResponseCode.FINISH;
    }

    private void updateChildren(Cluster webNode, List oldParentList, List newParentList) {
        Query query = new Query();
        query.eq(WebNode.PARENT, webNode.get(WebNode.ID));
        Update update = new Update();
        update.pullAll(WebNode.PARENT, oldParentList);
        update.push(WebNode.PARENT, newParentList, null, 0);
        webNodeDAO.update(query, update, false, true);
    }

    private String parent() {
        String id = form.getString("id");
        boolean result = Validate.check(Validate.OBJECTID, id);
        if (result) {
            ObjectId objectId = new ObjectId(id);
            Query query = new Query();
            query.ne(WebNode.PARENT, objectId);
            query.ne(WebNode.ID, objectId);
            Field field = new Field();
            field.include(WebNode.ID, WebNode.NAME, WebNode.PARENT_ID);
            List webNodes = webNodeDAO.find(query, field, null);
            context.put("webNodes", webNodes);
            return "parent";
        } else {
            LOGGER.error("非法查询栏目树：id=" + id);
            error(404);
            return NONE;
        }
    }
}
